<template>
  <div class="divBox relative">
    <el-card class="box-card mt14" :body-style="{ padding: '20px 20px' }" shadow="never" :bordered="false">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['platform:user:model:save']"
            >新增</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['platform:user:model:update']"
            >修改</el-button
          >
        </el-col>

        <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
      </el-row>

      <el-table v-loading="loading" :data="roomList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="模特类型" align="center" prop="modelType">
          <template slot-scope="scope">
            <!-- <dict-tag :options="modelTypeOptions" :value="scope.row.modelType"/>
             -->
            {{ scope.row.modelType | modelTypeFilter }}
          </template>
        </el-table-column>
        <el-table-column label="模特图片" align="center" prop="modelUrl">
          <template slot-scope="scope">
            <el-image
              style="width: 100px; height: 100px"
              :src="scope.row.modelUrl"
              :preview-src-list="[scope.row.modelUrl]"
            >
            </el-image>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['platform:user:model:update']"
              >修改</el-button
            >
            <!-- <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['platform:room:remove']"
              >删除</el-button
            > -->
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <!-- 添加或修改对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="模特类型" prop="modelType">
            <el-select v-model="form.modelType" placeholder="请选择模特类型">
              <el-option
                v-for="dict in modelTypeOptions"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="模特图片" prop="modelUrl">
            <div class="upLoadPicBox acea-row" @click="modalPicTap(false)">
              <div v-if="form.modelUrl" class="pictrue"><img :src="form.modelUrl" /></div>
              <div v-else class="upLoad">
                <i class="el-icon-camera cameraIconfont" />
              </div>
            </div>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { getUserModelAddressListApi, addUserModelAddressApi, updateUserModelAddressApi } from '@/api/model';
import dayjs from 'dayjs';
export default {
  name: 'Model',
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 模特列表
      roomList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 上传地址
      uploadUrl: process.env.VUE_APP_BASE_API + '/common/upload',
      // 模特类型选项
      modelTypeOptions: [
        { value: 0, label: '男' },
        { value: 1, label: '女' },
        { value: 2, label: '童' },
      ],
      // 查询参数
      queryParams: {
        limit: 10,
        page: 1,
      },
      // 表单参数
      form: {
        modelType: undefined,
        modelUrl: undefined,
      },
      formType: 'add',
      // 表单校验
      rules: {
        modelType: [{ required: true, message: '模特类型不能为空', trigger: 'change' }],
        modelUrl: [{ required: true, message: '模特图片不能为空', trigger: 'change' }],
      },
    };
  },
  created() {
    this.getList();
  },
  filters: {
    modelTypeFilter(value) {
      let option = [
        { value: 0, label: '男' },
        { value: 1, label: '女' },
        { value: 2, label: '童' },
      ];
      return option.find((item) => item.value === value).label;
    },
  },
  methods: {
    modalPicTap(multiple) {
      const _this = this;
      this.$modalUpload(
        function (img) {
          if (!img) return;
          console.log(img, 148);
          // _this.pram.imageUrl = img[0].sattDir;
          this.$set(_this.form, 'modelUrl', img[0].sattDir);
        },
        multiple,
        'content',
      );
    },
    /** 查询模特列表 */
    getList() {
      this.loading = true;
      if (this.dateRange && this.dateRange.length === 2) {
        this.queryParams.startTime = dayjs(this.dateRange[0]).format('YYYY-MM-DD HH:mm:ss');
        this.queryParams.endTime = dayjs(this.dateRange[1]).format('YYYY-MM-DD') + ' 23:59:59';
      } else {
        this.queryParams.startTime = '';
        this.queryParams.endTime = '';
      }
      getUserModelAddressListApi(this.queryParams).then((res) => {
        console.log(res, 186);
        this.roomList = res.list;
        this.total = res.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        modelType: undefined,
        modelUrl: undefined,
      };
      this.resetForm('form');
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = '添加模特';
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids[0];
      // TODO: 调用获取详情接口
      this.open = true;
      this.title = '修改模特';
      this.formType = 'edit';
      this.form = { ...row };
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          // TODO: 调用新增/修改接口
          if (this.formType === 'add') {
            addUserModelAddressApi(this.form).then((res) => {
              this.$message.success('新增成功');
              this.getList();
            });
          } else {
            updateUserModelAddressApi(this.form).then((res) => {
              this.$message.success('修改成功');
              this.getList();
            });
          }
          this.open = false;
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除所选数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        // TODO: 调用删除接口
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      // TODO: 调用导出接口
    },
    // 文件上传成功处理
    handleUploadSuccess(res, file) {
      if (res.code === 200) {
        this.form.modelUrl = res.url;
      } else {
        this.$message.error('上传失败');
      }
    },
    // 上传前图片格式校验
    beforeUpload(file) {
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/png';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传图片只能是 JPG/PNG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
  },
};
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
</style>